/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.financial.web.ttpaymentorder;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ar.entity.Tar;
import com.jeesite.modules.ar.entity.TarDetail;
import com.jeesite.modules.ar.service.TarService;
import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.service.cars.TcarService;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.custom.enumInfo.AccountType;
import com.jeesite.modules.financial.entity.ttpaymentorder.TtPaymentOrder;
import com.jeesite.modules.financial.entity.ttpaymentorder.TtPaymentOrderDetail;
import com.jeesite.modules.financial.service.ttpaymentorder.TtPaymentOrderService;
import com.jeesite.modules.logistics.entity.overseaswarehouse.OverseasWarehouse;
import com.jeesite.modules.logistics.entity.overseaswarehouse.OverseasWarehouseDetail;
import com.jeesite.modules.logistics.service.overseaswarehouse.OverseasWarehouseService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.payment.entity.Tap;
import com.jeesite.modules.payment.entity.TapDetail;
import com.jeesite.modules.payment.service.TapService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.service.EmpUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * TT付款单Controller
 * @author Kilo
 * @version 2021-03-10
 */
@Controller
@RequestMapping(value = "${adminPath}/financial/ttpaymentorder/ttPaymentOrder")
public class TtPaymentOrderController extends BaseController {

	@Autowired
	private TtPaymentOrderService ttPaymentOrderService;
	@Autowired
	private ServiceOrderService serviceOrderService;
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private OverseasWarehouseService overseasWarehouseService;
	@Autowired
	private TcarService tcarService;
	@Autowired
	private EmpUserService empUserService;
	@Autowired
	private TapService tapService;
	@Autowired
	private TarService tarService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TtPaymentOrder get(String ttPaymentOrderId, boolean isNewRecord) {
		return ttPaymentOrderService.get(ttPaymentOrderId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("financial:ttpaymentorder:ttPaymentOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(TtPaymentOrder ttPaymentOrder, Model model) {
		model.addAttribute("ttPaymentOrder", ttPaymentOrder);
		// 供应商
		List<Supplier> supplierList = supplierService.findList(new Supplier());
		model.addAttribute("supplierList", supplierList);
		// 海外资方
		Supplier hwzf = new Supplier();
		hwzf.setSupplierType("2");
		List<Supplier> hwzfList = supplierService.findList(hwzf);
		model.addAttribute("hwzfList", hwzfList);
		// 国内资方
		Supplier gnzf = new Supplier();
		gnzf.setSupplierType("1");
		List<Supplier> gnzfList = supplierService.findList(gnzf);
		model.addAttribute("gnzfList", gnzfList);
		// 车辆
		List<Tcar> tcarList = tcarService.findList(new Tcar());
		Tcar tcar = new Tcar();
		tcar.setCarName("");
		tcar.setCarId("");
		tcarList.add(tcar);
		model.addAttribute("tcarList", tcarList);
		model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
		model.addAttribute("listUser", empUserService.findList(new EmpUser()));
		return "modules/financial/ttpaymentorder/ttPaymentOrderList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("financial:ttpaymentorder:ttPaymentOrder:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TtPaymentOrder> listData(TtPaymentOrder ttPaymentOrder, HttpServletRequest request, HttpServletResponse response) {
		ttPaymentOrder.setPage(new Page<>(request, response));
		Page<TtPaymentOrder> page = ttPaymentOrderService.findPage(ttPaymentOrder);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("financial:ttpaymentorder:ttPaymentOrder:view")
	@RequestMapping(value = "form")
	public String form(TtPaymentOrder ttPaymentOrder, Model model) {
		model.addAttribute("ttPaymentOrder", ttPaymentOrder);
		// 供应商
		List<Supplier> supplierList = supplierService.findList(new Supplier());
		model.addAttribute("supplierList", supplierList);
		// 海外资方
		Supplier hwzf = new Supplier();
		hwzf.setSupplierType("2");
		List<Supplier> hwzfList = supplierService.findList(hwzf);
		model.addAttribute("hwzfList", hwzfList);
		// 国内资方
		Supplier gnzf = new Supplier();
		gnzf.setSupplierType("1");
		List<Supplier> gnzfList = supplierService.findList(gnzf);
		model.addAttribute("gnzfList", gnzfList);
		// 车辆
		List<Tcar> tcarList = tcarService.findList(new Tcar());
		Tcar tcar = new Tcar();
		tcar.setCarName("");
		tcar.setCarId("");
		tcarList.add(tcar);
		model.addAttribute("tcarList", tcarList);
		model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
		model.addAttribute("listOverseasWarehouse", overseasWarehouseService.findList(new OverseasWarehouse()));// 海外仓
		return "modules/financial/ttpaymentorder/ttPaymentOrderForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("financial:ttpaymentorder:ttPaymentOrder:view")
	@RequestMapping(value = "detail")
	public String detail(TtPaymentOrder ttPaymentOrder, Model model) {
		model.addAttribute("ttPaymentOrder", ttPaymentOrder);
		// 供应商
		List<Supplier> supplierList = supplierService.findList(new Supplier());
		model.addAttribute("supplierList", supplierList);
		// 海外资方
		Supplier hwzf = new Supplier();
		hwzf.setSupplierType("2");
		List<Supplier> hwzfList = supplierService.findList(hwzf);
		model.addAttribute("hwzfList", hwzfList);
		// 国内资方
		Supplier gnzf = new Supplier();
		gnzf.setSupplierType("1");
		List<Supplier> gnzfList = supplierService.findList(gnzf);
		model.addAttribute("gnzfList", gnzfList);
		// 车辆
		List<Tcar> tcarList = tcarService.findList(new Tcar());
		Tcar tcar = new Tcar();
		tcar.setCarName("");
		tcar.setCarId("");
		tcarList.add(tcar);
		model.addAttribute("tcarList", tcarList);
		model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
		model.addAttribute("listOverseasWarehouse", overseasWarehouseService.findList(new OverseasWarehouse()));// 海外仓
		return "modules/financial/ttpaymentorder/ttPaymentOrderDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("financial:ttpaymentorder:ttPaymentOrder:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TtPaymentOrder ttPaymentOrder) {
		ttPaymentOrderService.save(ttPaymentOrder);
		return renderResult(Global.TRUE, text("保存TT付款单成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("financial:ttpaymentorder:ttPaymentOrder:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TtPaymentOrder ttPaymentOrder) {
		ttPaymentOrderService.delete(ttPaymentOrder);
		return renderResult(Global.TRUE, text("删除TT付款单成功！"));
	}

	/**
	 * 海外仓TT付款按钮
	 */
	@RequiresPermissions("financial:ttpaymentorder:ttPaymentOrder:view")
	@RequestMapping(value = "goTT")
	public String goTT(OverseasWarehouse overseasWarehouse, Model model) {
		OverseasWarehouse bill = overseasWarehouseService.get(overseasWarehouse);
		TtPaymentOrder ttPaymentOrder = new TtPaymentOrder();
		ttPaymentOrder.setServiceOrderId(bill.getServiceOrderId());
		ttPaymentOrder.setCustomer(bill.getCustomer());
		ttPaymentOrder.setForeignBusinessmenId(bill.getForeignBusinessmenId());
		ttPaymentOrder.setInvestorId(bill.getInvestorId());
		Supplier supplier = supplierService.get(bill.getForeignBusinessmenId());
		ttPaymentOrder.setForeignBusinessmenBank(supplier.getTtAccountNumber());
		ttPaymentOrder.setForeignBusinessmenAddress(supplier.getAddress());
		ttPaymentOrder.setBank(supplier.getTtBankName());
		ttPaymentOrder.setBankAddress(supplier.getTtBankAddress());
		ttPaymentOrder.setSwiftCode(supplier.getSwiftNumber());
		ttPaymentOrder.setOverseasWarehouseCode(overseasWarehouse.getOverseasWarehouseId());
		ServiceOrder serviceOrder = serviceOrderService.get(bill.getServiceOrderId());
		ttPaymentOrder.setMarginRatio(serviceOrder.getMarginRatio());
		ttPaymentOrder.setCurrency(serviceOrder.getCurrency());

		List<OverseasWarehouseDetail> overseasWarehouseDetailList = bill.getOverseasWarehouseDetailList();
		List<TtPaymentOrderDetail> list = ListUtils.newArrayList();
		for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouseDetailList) {
			TtPaymentOrderDetail ttPaymentOrderDetail = new TtPaymentOrderDetail();
			ttPaymentOrderDetail.setCarId(overseasWarehouseDetail.getCarId());
			ttPaymentOrderDetail.setTcar(overseasWarehouseDetail.getTcar());
			ttPaymentOrderDetail.setModel(overseasWarehouseDetail.getModel());
			ttPaymentOrderDetail.setRowNumber(overseasWarehouseDetail.getRowNumber());
			ttPaymentOrderDetail.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
			list.add(ttPaymentOrderDetail);
		}
		ttPaymentOrder.setTtPaymentOrderDetailList(list);
		model.addAttribute("ttPaymentOrder", ttPaymentOrder);
		// 供应商
		List<Supplier> supplierList = supplierService.findList(new Supplier());
		model.addAttribute("supplierList", supplierList);
		// 海外资方
		Supplier hwzf = new Supplier();
		hwzf.setSupplierType("2");
		List<Supplier> hwzfList = supplierService.findList(hwzf);
		model.addAttribute("hwzfList", hwzfList);
		// 国内资方
		Supplier gnzf = new Supplier();
		gnzf.setSupplierType("1");
		List<Supplier> gnzfList = supplierService.findList(gnzf);
		model.addAttribute("gnzfList", gnzfList);
		// 车辆
		List<Tcar> tcarList = tcarService.findList(new Tcar());
		Tcar tcar = new Tcar();
		tcar.setCarName("");
		tcar.setCarId("");
		tcarList.add(tcar);
		model.addAttribute("tcarList", tcarList);
		model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
		model.addAttribute("listOverseasWarehouse", overseasWarehouseService.findList(new OverseasWarehouse()));// 海外仓
		return "modules/financial/ttpaymentorder/ttPaymentOrderForm";
	}

	/**
	 * 付款按钮
	 */
	@RequiresPermissions("financial:billnegotiated:billNegotiated:edit")
	@PostMapping(value = "goPay")
	@ResponseBody
	@Transactional(readOnly=false)
	public String goPay(TtPaymentOrder ttPaymentOrder) {
		TtPaymentOrder bill = ttPaymentOrderService.get(ttPaymentOrder);
		// 生成应付
		if(StringUtils.isEmpty(ttPaymentOrder.getApId())){
			Tap ap = new Tap();
			ap.setSupplierId(bill.getInvestorId());
			ap.setDate(new Date());
			ap.setStatus1("1");
			ap.setBillStatus("2");// 已提交
			ap.setServiceOrderId(bill.getServiceOrderId());
			ap.setAmount(bill.getTotal());
			ap.setCurrency(bill.getCurrency());
			ap.setParentBillType("TtPaymentOrder");
			ap.setParentBillCode(bill.getTtPaymentOrderId());
			List<TapDetail> list = ListUtils.newArrayList();
			for (TtPaymentOrderDetail ttPaymentOrderDetail : bill.getTtPaymentOrderDetailList()) {
				TapDetail detail = new TapDetail();
				detail.setCarId(ttPaymentOrderDetail.getCarId());
				detail.setFrameNumber(ttPaymentOrderDetail.getFrameNumber());
				detail.setModel(ttPaymentOrderDetail.getModel());
				detail.setExpenseItem(AccountType.TTK.getCode());//"ITM00000027");
				detail.setApAmount(ttPaymentOrderDetail.getRmbEquivalent());
				detail.setRowNumber(ttPaymentOrderDetail.getRowNumber());
				ServiceOrder serviceOrder = serviceOrderService.get(bill.getServiceOrderId());
				for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
					if(serviceOrderDetail.getFrameNumber().equals(ttPaymentOrderDetail.getFrameNumber())){
						detail.setBrand(serviceOrderDetail.getBrand());
						detail.setArea(serviceOrderDetail.getArea());
					}
				}
				list.add(detail);

				TapDetail detail1 = new TapDetail();
				detail1.setCarId(ttPaymentOrderDetail.getCarId());
				detail1.setFrameNumber(ttPaymentOrderDetail.getFrameNumber());
				detail1.setModel(ttPaymentOrderDetail.getModel());
				detail1.setExpenseItem(AccountType.TTDBF.getCode());//"ITM00000029");
				detail1.setRowNumber(ttPaymentOrderDetail.getRowNumber());
				detail1.setApAmount(ttPaymentOrderDetail.getTelegramFee());
				for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
					if(serviceOrderDetail.getFrameNumber().equals(ttPaymentOrderDetail.getFrameNumber())){
						detail1.setBrand(serviceOrderDetail.getBrand());
						detail1.setArea(serviceOrderDetail.getArea());
					}
				}
				list.add(detail1);

				TapDetail detail2 = new TapDetail();
				detail2.setCarId(ttPaymentOrderDetail.getCarId());
				detail2.setFrameNumber(ttPaymentOrderDetail.getFrameNumber());
				detail2.setModel(ttPaymentOrderDetail.getModel());
				detail2.setExpenseItem(AccountType.TTSXF.getCode());//"ITM00000028");
				detail2.setRowNumber(ttPaymentOrderDetail.getRowNumber());
				detail2.setApAmount(ttPaymentOrderDetail.getHandlingFee());
				for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
					if(serviceOrderDetail.getFrameNumber().equals(ttPaymentOrderDetail.getFrameNumber())){
						detail2.setBrand(serviceOrderDetail.getBrand());
						detail2.setArea(serviceOrderDetail.getArea());
					}
				}
				list.add(detail2);
			}
			ap.setTapDetailList(list);
			tapService.save(ap);
			ttPaymentOrder.setApId(ap.getApId());
			ttPaymentOrderService.update(ttPaymentOrder);
		}

		// 生成应收
		if(StringUtils.isEmpty(ttPaymentOrder.getArId())){
			Tar ar = new Tar();
			ar.setCustomer(bill.getCustomer());
			ar.setCustomerId(bill.getCustomer().getCustomerId());
			ar.setDate(new Date());
			ar.setStatus1("1");
			ar.setBillStatus("2");// 已提交
			ar.setServiceOrderId(bill.getServiceOrderId());
			ar.setAmount(bill.getTotal());
			ar.setCurrency(bill.getCurrency());
			ar.setParentBillType("TtPaymentOrder");
			ar.setParentBillCode(bill.getTtPaymentOrderId());
			List<TarDetail> list = ListUtils.newArrayList();
			for (TtPaymentOrderDetail ttPaymentOrderDetail : bill.getTtPaymentOrderDetailList()) {
				TarDetail detail = new TarDetail();
				detail.setCarId(ttPaymentOrderDetail.getCarId());
				detail.setFrameNumber(ttPaymentOrderDetail.getFrameNumber());
				detail.setModel(ttPaymentOrderDetail.getModel());
				detail.setExpenseItem(AccountType.TTK.getCode());//"ITM00000027");
				detail.setApAmount(ttPaymentOrderDetail.getRmbEquivalent());
				detail.setRowNumber(ttPaymentOrderDetail.getRowNumber());
				ServiceOrder serviceOrder = serviceOrderService.get(bill.getServiceOrderId());
				for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
					if(serviceOrderDetail.getFrameNumber().equals(ttPaymentOrderDetail.getFrameNumber())){
						detail.setBrand(serviceOrderDetail.getBrand());
						detail.setArea(serviceOrderDetail.getArea());
					}
				}
				list.add(detail);

				TarDetail detail1 = new TarDetail();
				detail1.setCarId(ttPaymentOrderDetail.getCarId());
				detail1.setFrameNumber(ttPaymentOrderDetail.getFrameNumber());
				detail1.setModel(ttPaymentOrderDetail.getModel());
				detail1.setExpenseItem(AccountType.TTDBF.getCode());//"ITM00000029");
				detail.setRowNumber(ttPaymentOrderDetail.getRowNumber());
				detail1.setApAmount(ttPaymentOrderDetail.getTelegramFee());
				for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
					if(serviceOrderDetail.getFrameNumber().equals(ttPaymentOrderDetail.getFrameNumber())){
						detail1.setBrand(serviceOrderDetail.getBrand());
						detail1.setArea(serviceOrderDetail.getArea());
					}
				}
				list.add(detail1);

				TarDetail detail2 = new TarDetail();
				detail2.setCarId(ttPaymentOrderDetail.getCarId());
				detail2.setFrameNumber(ttPaymentOrderDetail.getFrameNumber());
				detail2.setModel(ttPaymentOrderDetail.getModel());
				detail2.setExpenseItem(AccountType.TTSXF.getCode());//"ITM00000028");
				detail2.setRowNumber(ttPaymentOrderDetail.getRowNumber());
				detail2.setApAmount(ttPaymentOrderDetail.getHandlingFee());
				for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
					if(serviceOrderDetail.getFrameNumber().equals(ttPaymentOrderDetail.getFrameNumber())){
						detail2.setBrand(serviceOrderDetail.getBrand());
						detail2.setArea(serviceOrderDetail.getArea());
					}
				}
				list.add(detail2);
			}
			ar.setTarDetailList(list);
			tarService.save(ar);
			ttPaymentOrder.setApId(ar.getArId());
			ttPaymentOrderService.update(ttPaymentOrder);
		}

		return renderResult(Global.TRUE, text("付款成功！"));
	}
}